package com.hopster;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AlarmPage extends Activity
{
	private LinearLayout layout;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_page);
        
        Button createAlarmButton = (Button) findViewById(R.id.createAlarm);
        
        layout = (LinearLayout)findViewById(R.id.alarm_page_layout);
        
        createAlarmButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		Intent i = new Intent();
        		i.setClassName("com.hopster", "com.hopster.CreateAlarm");
        		startActivity(i);
        	}
        });
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        
        AlarmService as = AlarmService.getInstance();
        List<Alarm> alarms = as.getAlarms();
        
        try
        {
	        while(true)
	        {
	        	layout.removeViewAt(1);
	        }
        }
        catch(Exception e) { }
        
        for(Alarm alarm : alarms)
        {
        	TextView textView = new TextView(AlarmPage.this);
        	final String name = alarm.getName();
        	final int hour = alarm.getHour();
        	int hour2 = hour;
        	final int minute = alarm.getMinute();
        	String am_pm;
        	
        	// Change from 24-hour clock notation to 12-hour notation w/ AM/PM
        	if(hour<12)
        		am_pm = "AM";
        	else
        		am_pm = "PM";
        	
        	if(hour2==0)
        		hour2=12;
        	if(hour2>12)
        		hour2 = hour2-12;
        	
        	String sMinute = (minute == 0) ? "00" : 
        					  (minute < 10) ? "0" + minute
        									: "" + minute;
        	
        	textView.setText(name + " " + hour2 + ":" + sMinute + " " + am_pm);
        	textView.setTextSize(30f);
        	layout.addView(textView);
        	textView.setOnClickListener(new View.OnClickListener()
        	{
				
				public void onClick(View v) 
				{
					Intent i = new Intent();
            		Bundle alarms = new Bundle();
            		alarms.putString("name", name);
            		alarms.putInt("hour", hour);
            		alarms.putInt("minute", minute);
            		i.putExtras(alarms);
	        		i.setClassName("com.hopster", "com.hopster.EditAlarm");
	        		startActivity(i);
				}
			});
        }
    }
}
